Docker Notes 2 - Basic Usage
Basic usage methods of Docker
Containerization isolates different environments, allowing engineers to focus on development without spending excessive effort on environment setup.
Docker is currently a mature and popular containerization technology.
Exploring Docker Commands
View Container Information
docker ps List all running containers
docker ps -a List all existing containers
View Logs
docker logs [CONTAINER] Print logs
docker logs -f [CONTAINER] Continuously print the latest logs
Run Containers
Create a container from a local environment image. If it doesn’t exist, access the repository.
A “login” action is required for private repos. The image format can be [IMAGE NAME]:[TAG]
docker run
Examples:
# Run nginx
docker run -idt nginx
# Run nginx and expose port 8080
docker run -idt -p 8080:80 nginx
# Run nginx on port 8080, automatically restart on failure
docker run -idt -p 8080:80 --restart on-failure nginx
# Run nginx and remove after termination
docker run --rm -idt nginx
Commonly Used Flags
-d
detach: Run the container in the background-i
interactive: Keep STDIN open, allowing interaction-t
allocate a pseudo-TTY: Allow the container to use a TTY-p
expose socket: Host Port:Docker Port (e.g., -p 8080:80)--restart
: Restart policy (always, none, on-failure)--rm
: Automatically remove the container after closing; otherwise, it may accumulate even if it’s dead
Common Commands
Enter a running container and open a terminal command interface
docker exec -ti [CONTAINER] bash
Get metadata of a container or image
docker inspect [CONTAINER]
Delete Docker container
docker rm [CONTAINER]
Delete Docker image
docker rmi [IMAGE]
View running processes in a container
docker top [CONTAINER]
Other Commands
Build Docker image using Dockerfile
-f
: Manually specify the Dockerfile name
# Build image in the current directory using Dockerfile
docker build . -t image-name
# Build image in the current directory using Dockerfile-alt
docker build . -t image-name -f Dockerfile-alt
Pull/Push docker image from/to repository Login to Docker Hub first
docker push / pull
Login to Docker repository
docker login
How to Write Your Dockerfile
- Register on Docker Hub first
- Alpine Linux
- Very small size (5MB)
- Complete Linux architecture
- Rich component support
Common Dockerfile Commands
Basic
- FROM: Specify the base image; defaults to latest if no version is specified
- RUN:
- Commands executed during docker build to install dependencies.
- Each RUN is a separate event.
- ARG:
- Build parameters during container docker build.
- Cannot be used in containers.
- ENV:
- Environment variables used inside the container.
- Format:
ENV <key> <value>
orENV <key1>=<value1> <key2>=<value2>
File Copy
- COPY:
- Copy “source file/directory” to “file/directory” in the container
COPY [--chown=<user>:<group>] <source path>... <dist path>
- Dist path can be either an absolute path inside the Container or relative path to the WORKDIR
- Preserves various attributes of the source data
- ADD:
- Enhanced version of COPY
ADD [--chown=<user>:<group>] <source path>... <dist path>
- Allows source path to be a URL
- File permissions are automatically set to
600
Execution
- ENTRYPOINT:
- Default command executed when the container starts
- The last command, should keep the container running in the foreground
- CMD:
- Default command (or parameters) executed when the container starts
- If both ENTRYPOINT and CMD exist, CMD is used as a parameter
FROM ubuntu
ENTRYPOINY ["ping"]
CMD ["localhost"]
WORKDIR(cd + mkdir):
- Initial working directory inside the container
Command Differences
ARG vs ENV
ARG is passed during image build
docker build . --build-arg FOO=bar
ENV is used as environment variables inside the Docker container
CMD vs ENTRYPOINT
ENTRYPOINT is executed by default when the container starts CMD is used as parameters if ENTRYPOINT exists
FROM ubuntu
ENTRYPOINY ["ping"]
CMD ["localhost"]
COPY vs ADD
COPY is used to copy files from the local machine to the container ADD is used to copy files from remote locations to the container
Debugging
docker: Got permission denied
This error occurs because the current user lacks permissions.
You can elevate the user to root
to ensure permissions.
However, a safer and correct approach is to:
- Create a docker execution group
- Add the user to the docker group
Commands below, need to logout and login again to take effect!
sudo groupadd docker
sudo usermod -aG docker user_name