34

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

2

5 Answers 5

33

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.

Sign up to request clarification or add additional context in comments.

Comments

12

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.

Comments

5
  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.

1 Comment

Note: for me the command was docker exec -it <docker_it> /bin/bash
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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.