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
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.
To access the service from inside the container you need the port that particular host is listening to as no matter where the service is running we need to access it through the host node. If you have a service running on some other port you can access it via 172.17. 42.1:5432.
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.
docker ps
' to get the names and docker id for your container.docker exec -it <docker_id> /bin/bash
'this will give you bash prompt inside container.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With