List of Docker commands that I often use (container operation)

Introduction

I was trying to remember by looking at the official Docker manual when I was operating various things. I started to do various things and I couldn't remember it anymore. I made a list of frequently used commands and wrote them out.

Container creation operation related

** I've listed some of the most commonly used ones below, but there are more run options, so ** ** Please refer to Docker URL. ** **

  1. Option to restart automatically when the host is restarted or the container is stopped unintentionally
docker run --restart=always
  1. Mount the container volume on the host
docker run -v [Volume path on the host side]:[Path in the container] [The name of the container image you want to start]
  1. Access the container from the outside (port forwarding) You can specify more than one
docker run -p [External port]:[Container port]

Login to container and command execution

  1. Login to the container
docker exec -it [Container ID or container name] /bin/bash
  1. Command execution in the container
docker exec [Container ID or container name] [Command you want to execute]

Container status check operation

  1. Display a list of currently running containers Check if the STATUS column is UP to see if it is running
docker ps
  1. Display a list of stopped / running containers
docker ps -a

Check container log

  1. It's like displaying the container log with cat
docker logs [Container name or container ID]
  1. Add the time when outputting the log
docker logs -t [Container name or container ID]
  1. Continue to display logs in real time
docker logs -f [Container name or container ID]
  1. Display the specified number of lines from the end of the log
docker logs --tail=[Number of lines to display] [Container name or container ID]

Delete container operation

  1. Delete the stopped container
docker rm [Container name or container ID]
  1. Delete the running container
docker rm -rf [Container name or container ID]
  1. Delete all stopped containers Extract all the containers stopped with -a, extract the container ID from them with -q, and delete it.
docker rm $(docker ps -q -a)

Container stop / start / restart operation

  1. Stop a specific container that is running
docker stop [Container name or container ID]
  1. Start a specific container that is stopped
docekr start [Container name or container ID]
  1. Restart a specific container
docekr restart [Container name or container ID]
  1. Stop all containers Extract container ID with -q and stop container
docker stop $(docker ps -q)
  1. Start all containers Extract the container ID with -q and start the container
docker start $(docker ps -q)

Docker operation Other

  1. Deletes all stopped containers, unused networks, unused images, etc. However, please be careful when executing it as it will be deleted all at once. .. ..
docker system purne
  1. Display the detailed information of the created container or image name
docker inspect [Container name or container ID or image ID]
  1. Change the container name
docker rename [Container name before change] [The changed container name]

Finally

This time, I mainly touched on creating and deleting containers in Docker. When I write next time, I would like to write something that goes a little further.

Recommended Posts

List of Docker commands that I often use (container operation)
Summary of Docker commands that I often use but can't remember
[Note] A list of commands that I checked many times after trying Ruby.
About simple operation of Docker
[Docker] List of errors that occurred when building the environment
Nginx container that displays the Hostname of the host running Docker
[Ruby] List of basic commands
Know the convenience of Docker (-compose) now (information list that I referred to when using it)
I created a Docker image of a container for learning OpenAI Gym
Summary of frequently used Docker commands
[Docker] Operation up to container creation # 2
The story of updating SonarQube's Docker Container
Introduction of Docker Hub and commands Self-learning ①
Japanese setting of mysql in Docker container
Commands that helped resolve errors in Docker