Docker Command Line Workout 💪🏼

Docker is cool.

This is the exercise to build your muscle memory for docker commands. Type them in the terminal like a pro. It makes you look good. It makes you feel good. It makes everybody feel good. 🥳

This is for building docker command muscle memory. If you know these commands by heart, you can be the one who is saving the day when the production is down or helping your colleagues with their docker issues.

I hope this helps you to be the docker hero🤟💀🤟

Feel free to reach out to me🤙

Blog || Open Source Project || Band Camp

If you want to make a suggestion or contribute to this, feel free to pull the repo and make a pull request!


Topics🏷

(1) Interacting with a running container

(2) Building containers

(3) Docker compose



(1) INTERACTING WITH CONTAINER


(1) Checking container stats

Check which container is using how much CPU or Memory.

Answer docker stats displays a live stream of container resource usage statistics
docker stats

(2) Streaming container log

Stream container log in the terminal.

Answer -f will allow you to stream the log.
docker logs -f <Container Name>

(3) Listing containers

List

  • all the existing containers
  • all the existing images
  • all the running containers
Answer
docker container list --all

docker images -a
# or docker image ls

docker ps

(4) Pruning containers in the local machine

Remove the stopped container and all of the images, including unused or dangling images

Answer
docker system prune -a

(5) Getting into Docker's container shell

Get into docker's container shell with interactive mode. Then exit.

Answer
# -i: Interactive
# -t: Allocate a pseudo-TTY
docker exec -it <container-name> bash # this works most of the time

# some containers need to do it like this
docker exec -it <container-name>  /bin/sh  # e.g node alpine containers
docker exec -it <container-name>  /bin/bash

# then exit
exit

(5) Getting into Docker's container shell

Get into docker's container shell with interactive mode. Then exit.

Answer
# -i: Interactive
# -t: Allocate a pseudo-TTY
docker exec -it <container-name> bash # this works most of the time

# some containers need to do it like this
docker exec -it <container-name>  /bin/sh  # e.g node alpine containers
docker exec -it <container-name>  /bin/bash

# then exit
exit

(2) BUILDING CONTAINERS


(1) Build and run from Dockerfile

Build a container with container name called my-app from the Dockerfile below with port mapping of 3000 to 3000. Then, restart the container.

FROM node:12-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "/app/src/index.js"]
Answer
# Build the image
docker build -t my-app .

# Start the container with port mapped to 3000.
docker run -dp 3000:3000 my-app

# Stop the container
docker stop my-app

# Start the container
docker start my-app

(2) Stop the container and remove

Stop the existing container named my-app and remove it.

Answer
# -f will stop and remove the container
docker rm <container-id or name> -f

# Alternatively stop and remove
docker stop <container-id or name>
docker rm <container-id or name>

(3) DOCKER-COMPOSE


(1) Starting all the existing containers

Starting all the existing container created by docker-compose.

Answer
docker-compose start

(2) Stop all the existing containers

Stopping all the existing container created by docker-compose.

Answer
docker-compose stop

(3) Rebuilding the containers

After updating docker file or docker-compose file, rebuild the containers.

Answer

This will update the container and restart the container. It will take up the terminal process. If you do ctrl + c, it will stop all the containers.

docker-compose up --build

Of course, we can run container in a detached mode. This will keep all the container running.

docker-compose up --build -d

(4) Building one container from a particular docker-compose file

Build only app service from docker-compose.ci.yml

Answer

We can just add the service name as blow. -f is to specify which file. -d is in detached mode.

docker-compose -f docker-compose.ci.yml up app -d