Docker-Compose Create / Start / Stop / Rm / Up / Down
Let’s see the basic command of docker-compose,
docker-compose --help
will list all the options available for the docker-compose command.
Docker-compose create
This command is used to create a container and it will neither create the network and volume nor will start them, it will just create. As shown below output, the docker-compose create command is deprecated and it is suggested to use docker-compose up --no-start flag.
output:
gaurav@learning-ocean:~/docker-compose$ docker-compose create
WARNING: The create command is deprecated. Use the up command with the --no-start flag instead.
Creating docker-compose_webapp1_1 ... done
Creating docker-compose_webapp2_1 ... done
gaurav@learning-ocean:~/docker-compose$ docker network ls
NETWORK ID NAME DRIVER SCOPE
0022a9f511fe bridge bridge local
3a33f83c3663 host host local
e4ebd601732c none null local
gaurav@learning-ocean:~/docker-compose$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
gaurav@learning-ocean:~/docker-compose$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
474f9071d846 nginx "/docker-entrypoint.…" 16 seconds ago Created docker-compose_webapp2_1
9cae49d2eb8e nginx "/docker-entrypoint.…" 16 seconds ago Created docker-compose_webapp1_1
gaurav@learning-ocean:~/docker-compose$
So, if we run docker-compose up --no-start flag then it will also create the container but it will also create the default network
[email protected]:~/docker-compose$ docker-compose up --no-start
Creating network "docker-compose_default" with the default driver
Creating docker-compose_webapp1_1 ... done
Creating docker-compose_webapp2_1 ... done
[email protected]:~/docker-compose$
docker-compose start
this command will start the container as shown below
[email protected]:~/docker-compose$ ls
docker-compose.yaml
[email protected]:~/docker-compose$ docker-compose up --no-start
Creating network "docker-compose_default" with the default driver
Creating docker-compose_webapp1_1 ... done
Creating docker-compose_webapp2_1 ... done
[email protected]:~/docker-compose$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29dff16ec850 nginx "/docker-entrypoint.…" 3 seconds ago Created docker-compose_webapp2_1
b23af0a13110 nginx "/docker-entrypoint.…" 3 seconds ago Created docker-compose_webapp1_1
[email protected]:~/docker-compose$ docker-compose start
Starting webapp1 ... done
Starting webapp2 ... done
[email protected]:~/docker-compose$
docker-compose stop
will stop the container
[email protected]:~/docker-compose$ docker-compose stop
Stopping docker-compose_webapp2_1 ... done
Stopping docker-compose_webapp1_1 ... done
[email protected]:~/docker-compose$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29dff16ec850 nginx "/docker-entrypoint.…" About a minute ago Exited (0) 4 seconds ago docker-compose_webapp2_1
b23af0a13110 nginx "/docker-entrypoint.…" About a minute ago Exited (0) 4 seconds ago docker-compose_webapp1_1
[email protected]:~/docker-compose$
docker-compose rm
will delete the container but it will not remove the network and volume
gaurav@learning-ocean:~/docker-compose$ docker-compose rm
Going to remove docker-compose_webapp2_1, docker-compose_webapp1_1
Are you sure? [yN] yes
Removing docker-compose_webapp2_1 ... done
Removing docker-compose_webapp1_1 ... done
gaurav@learning-ocean:~/docker-compose$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
gaurav@learning-ocean:~/docker-compose$ docker network ls
NETWORK ID NAME DRIVER SCOPE
0022a9f511fe bridge bridge local
6866a6234089 docker-compose_default bridge local
3a33f83c3663 host host local
e4ebd601732c none null local
[email protected]:~/docker-compose$
docker-compose down
will delete the container along with the network and it can also delete the volume if provided with --volume flag
[email protected]:~/docker-compose$ docker-compose up -d
Creating docker-compose_webapp1_1 ... done
Creating docker-compose_webapp2_1 ... done
[email protected]:~/docker-compose$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ab41095f13c nginx "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:8000->80/tcp docker-compose_webapp1_1
d1a042047493 nginx "/docker-entrypoint.…" 7 seconds ago Up 4 seconds 0.0.0.0:8002->80/tcp docker-compose_webapp2_1
[email protected]:~/docker-compose$ docker-compose down
Stopping docker-compose_webapp1_1 ... done
Stopping docker-compose_webapp2_1 ... done
Removing docker-compose_webapp1_1 ... done
Removing docker-compose_webapp2_1 ... done
Removing network docker-compose_default
[email protected]:~/docker-compose$ docker network ls
NETWORK ID NAME DRIVER SCOPE
0022a9f511fe bridge bridge local
3a33f83c3663 host host local
e4ebd601732c none null local
[email protected]:~/docker-compose$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[email protected]:~/docker-compose$