Scenario:
With the following docker-compose.yaml
version: 3
services:
helloworld:
image: hello-world
container_name: hello-world
whoami:
image: containous/whoami
container_name: whoami
containers are started with docker-compose up
docker-compose.yaml is then edited to expose a port
version: 3
services:
helloworld:
image: hello-world
container_name: hello-world
whoami:
image: containous/whoami
container_name: whoami
ports:
- 10000:80
whoami is restarted via docker-compose restart whoami
Problem: the port is not exposed.
My question: what is the correct command to restart a container (previouly started as part of a docker-compose up) so that its (modified) definition in docker-compose.yaml is taken into account?
Note: restarting everything with docker-compose down && docker-compose up correctly exposes the port. What I want to avoid is to interfere with other running containers when modifying a single one.
Only another docker-compose up seems to work.
According to docker-compose up documentation:
If there are existing containers for a service, and the service’s configuration or image was changed after the container’s creation, docker-compose up picks up the changes by stopping and recreating the containers (preserving mounted volumes).
Related
i am running docker on windows 10, and have a jenkins container
i can use container jenkins pipeline to build host image
docker -H host.docker.internal:2375 tag myproject:1.0 myproject:latest
i can start host container use docker-compose
docker-compose -H host.docker.internal:2375 -f /var/jenkins_home/myproject/docker-compose.yml up -d
the only issue is, if have 'volumes' in docker-compose.yml, it will display error below.
Named volume "C:\docker\myproject:/target/myproject" is used in service "myproject" but no declaration was found in the volumes section.
docker-compose.yml file
version: '3.9'
services:
myproject:
image: myproject:latest
user: root
container_name: myproject
volumes:
- C:\docker\myproject:/target/myproject
ports:
- 8080:8080
i understand it is because jenkins container cannot found 'C:\docker\myproject', but i want share this folder between host and myproject container.
i tried use below command in jenkins container but it is not working, -f only can read local container file
docker-compose -H host.docker.internal:2375 -f c:/myproject/docker-compose.yml up -d
any idea can run docker-compose with volumes in jenkins container to control host docker?
update problem solved by below
version: '3.9'
services:
myproject:
image: myproject:latest
user: root
container_name: myproject
volumes:
- type: bind
source: C:\docker\myproject
target: /target/myproject
ports:
- 8080:8080
I want to execute a command using of a docker-compose file, and the code sometimes fails because of connection timeouts. I thought that adding restart: on-failure would automatically restart the container if it failed.
The command looks like that
docker-compose run --rm \
-e VAR1=value1 \
[...] \
web flask tasks my_failing_task
My docker-compose.yml looks like that
version: "3"
services:
web:
user: root
image: my-image
network_mode: "host"
environment:
APPLICATION: "web"
GOOGLE_APPLICATION_CREDENTIALS: "mysecret.json"
volumes:
- ../../../stuff/:/stuff/
restart: on-failure:3
I have noticed that the container does not restart when I use docker-compose run.
I have then tried to move the command inside the docker-compose.yml, like this:
version: "3"
services:
web:
user: root
image: my-image
network_mode: "host"
command: flask tasks my_failing_task
environment:
APPLICATION: "web"
GOOGLE_APPLICATION_CREDENTIALS: "mysecret.json"
VAR1: value1
volumes:
- ../../../stuff/:/stuff/
restart: on-failure:3
And execute docker-compose up, but same result.
It seems that restart only works with docker-compose up when I add another container, like a redis for example
version: "3"
services:
web:
user: root
image: my-image
network_mode: "host"
command: flask tasks my_failing_task
environment:
APPLICATION: "web"
GOOGLE_APPLICATION_CREDENTIALS: "mysecret.json"
VAR1: value1
volumes:
- ../../../stuff/:/stuff/
restart: on-failure:3
redis:
hostname: redis
image: redis
ports:
- "6379:6379"
Then it actually restarts up to 3 times if fails.
So my questions are:
Why doesn't restart work with run
Why does restart only work with up IF there are more than 1 container in the docker-compose.yml file
Thanks!
In the code, docker-compose run always implies restart: no. The GitHub issue docker/compose#6302 describes this a little bit further.
docker-compose run is used to run a "one-off" container, running a single alternate command with mostly the same specification as what's described in docker-compose.yml. Imagine Compose didn't have this override. If you did docker-compose run a command that failed, it would restart, potentially forever. If it ran in the background, you'd leak a restarting container; if it ran in the foreground, you'd have to go to another terminal to kill off the container. That's a harsh penalty for typing docker-compose run web bsah by accident.
Otherwise, most of the options, including restart:, get passed through directly to the Docker API. It shouldn't make a difference running docker-compose up if there's only one container or multiple.
Below is my docker compose file :
version: "3.5"
services:
docker-mysql:
image: mysql
container_name: docker-mysql
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=user_management
networks:
- technet
networks:
technet:
name: technet
Now I want to start the MySQL container in technet network with the name docker-mysql. Next, I will change the network to technet2 in the docker-compose.yml manually and username and password but start with same container and image name.
Currently, the behavior is the old container in the technet is stopped and rebuilt inside the technet2 network. But I want to have 2 containers of MySQL with the same container name docker-mysql but in a different network i.e. technet and technet2.
I'm trying to run two services using docker compose. Here is my docker-compose.yml:
version: '3'
services:
data:
build: ./ML_py
ports:
- "5000:5000"
node:
build: ./node_server
ports:
- "8080:8080"
This is all identical to the docker-compose beginner documentation, and when I run docker-compose up a network is created for the set of services. However, when I run docker ps, the containers have no mapped ports. Shouldn't the port values specified by 'ports' take care of this? Anyone know what might cause this?
I am trying to use Docker Compose (with Docker Machine on Windows) to launch a group of Docker containers.
My docker-compose.yml:
version: '2'
services:
postgres:
build: ./postgres
environment:
- POSTGRES_PASSWORD=mysecretpassword
frontend:
build: ./frontend
ports:
- "4567:4567"
depends_on:
- postgres
backend:
build: ./backend
ports:
- "5000:5000"
depends_on:
- postgres
docker-compose build runs successfully. When I run docker-compose start I get the following output:
Starting postgres ... done
Starting frontend ... done
Starting backend ... done
ERROR: No containers to start
I did confirm that the docker containers are not running. How do I get my containers to start?
The issue here is that you haven't actually created the containers. You will have to create these containers before running them. You could use the docker-compose up instead, that will create the containers and then start them.
Or you could run docker-compose create to create the containers and then run the docker-compose start to start them.
The reason why you saw the error is that docker-compose start and docker-compose restart assume that the containers already exist.
If you want to build and start containers, use
docker-compose up
If you only want to build the containers, use
docker-compose up --no-start
Afterwards, docker-compose {start,restart,stop} should work as expected.
There used to be a docker-compose create command, but it is now deprecated in favor of docker-compose up --no-start.