32

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

Vikas Rathore
  • 8,242
  • 8
  • 35
  • 54
  • Maybe this will help you: [How to get into a docker container?](https://stackoverflow.com/questions/30172605/how-to-get-into-a-docker-container) – tgogos May 16 '18 at 09:18
  • 1
    Possible duplicate of [How to get into a docker container?](https://stackoverflow.com/questions/30172605/how-to-get-into-a-docker-container) – k0pernikus May 16 '18 at 09:20

5 Answers5

30

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.

Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62
8

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.

hailong
  • 1,409
  • 16
  • 19
3
  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.

Prateek Jain
  • 2,738
  • 4
  • 28
  • 42
2

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.

smcjones
  • 5,490
  • 1
  • 23
  • 39
2

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