Vikas Rathore
Vikas Rathore

Reputation: 8801

How to ssh into the services create using docker-compose

This is my docker compose file

version: '2'

# based off compose-sample-2, only we build nginx.conf into image
# uses sample site from https://startbootstrap.com/template-overviews/agency/

services:
  proxy:
    build:
      context: .
      dockerfile: nginx.Dockerfile
    ports:
      - '80:80'
  web:
    image: httpd
    volumes:
      - ./html:/usr/local/apache2/htdocs/

Now can I ssh into any of the services which gets creats when I run docker-compose up

Upvotes: 34

Views: 52313

Answers (5)

Prateek Jain
Prateek Jain

Reputation: 3045

  1. do 'docker ps' to get the names and docker id for your container.
  2. do 'docker exec -it <docker_id> /bin/bash'

this will give you bash prompt inside container.

Upvotes: 5

hailong
hailong

Reputation: 1515

The easiest way is to run the docker-compose exec command:

docker-compose exec web /bin/bash

with the latest version of docker, since "Docker Compose is now in the Docker CLI", you can do:

docker compose exec web /bin/bash

If you do want to use the pure docker command, you can do:

web=`docker container ls |grep web |awk '{print \$1}'`
docker container exec -it $web /bin/bash

or in a single line:

docker container exec -it `docker container ls |grep web |awk '{print \$1}'` /bin/bash

in which we find the container id first, then run the familiar docker container exec -it command.

As it is mentioned in the question, the service containers have been created by running docker-compose up. Therefore I don't think docker-compose run is appropriate answer, since it will start a container in my test.

Upvotes: 12

While using the docker command also works, the easiest way to do this which does not require finding out the ID of the container is using the docker-compose run subcommand, for example:

service="web"
docker-compose run $service /bin/bash

Upvotes: 2

smcjones
smcjones

Reputation: 5600

If you specify container_name in your docker-compose.yaml file, you can use that to log in with the docker exec command.

Example:

django:
    container_name: django
    more_stuff: ...

Then, to log in to a running docker-compose:

docker exec -it django /bin/bash

This works better for me as I don't need to check the current running ID and it's easy for me to remember.

Upvotes: 1

Oleg Sklyar
Oleg Sklyar

Reputation: 10082

The standard mechanism is not to ssh into containers, but to connect to a container using docker exec. Given a container Id like 3cdb7385c127 you can connect (aka ssh into it) with

docker exec -it 3cdb7385c127 sh

or for a full login shell if you have bash available in container

docker exec -it 3cdb7385c127 bash -l

You can still ssh into a container if you want to, but you would need to have a ssh server installed, configured and running and you would need to have access to the container IP from outside or redirect container's :22 onto some port on the host.

Upvotes: 33

Related Questions