Docker for Linux
Docker for Linux
Docker is one of the main containerization tools for Linux.
Common Commands for Getting Things Done.
run
The docker run command creates a new container from an image and starts a process inside the new container. If the container image is not available, this command will also try to download it:
$ docker run [options] <image> [command]
Example:
$ docker run --name test-db mysql bash
Options:
-- name What you want the container to be known as. If omitted, a randomly generated name is created by docker.
-d daemon mode. Launches the container in the background and returns you to the system prompt.
-it This is used if the command you are running needs to be interactive and is text based. Not use with -d
-e This is used to pass any environment variables to the container.
-v This is used to specify persistent storage for a container. See Mounting Persistent Storage
--volumes-from This is used to mount all storage from an already running container as the new container's storage.
--rm This tells docker to automatically remove the container when it stops.
-p This is used to do port forwarding from the host to the container. See Port Forwarding
Command: This is any command that is in the image you are trying to run. If excluded, the default command defined by the image creation process will be executed.
ps
ps shows you what containers are running.
$ docker ps [options]
Example:
$ docker ps
Options:
--all Includes containers that are stopped.
-a Same as --all
-q Returns only the container IDs.
exec
exec executes a command in an already running container
$ docker exec [options] <container name|container ID> <command>
Example:
$ docker exec -it test-db /bin/bash
Options:
-it This is used if the command you are running needs to be interactive and is text based.
Tip: You can get the name of the container from the ps command.
logs
logs displays the runtime logs from a running container.
$ docker logs <container name|container ID>
Example:
$ docker logs test-db
stop
stop tells docker to gracefully shutdown the named container.
$ docker stop <container name|container ID>
Example: Stopping one container
$ docker stop test-db
Example: Stopping all running containers
$ docker stop $(docker ps -q)
Tip: You can get the name of the container from the ps command.
kill
kill tells docker to forcefully shutdown the named container.
$ docker kill [options] <container name|container ID>
Example:
$ docker kill test-db
Options:
-s This is a subset of the signals available to the OS kill command. The default signal is SIGKILL
Signal | Default Action | Description |
---|---|---|
SIGHUP | Terminate process | Terminate line hangup |
SIGINT | Terminate process | Interrupt program |
SIGQUIT | Create core image | Quit program |
SIGABRT | Create core image | Abort program |
SIGKILL | Terminate process | Kill program |
SIGTERM | Terminate process | Software termination signal |
SIGUSR1 | Terminate process | User-defined signal 1 |
SIGUSR2 | Terminate process | User-defined signal 2 |
Tip: You can get the name of the container from the ps command. |
restart
restart tells docker to start a previously stopped container.
$ docker restart <container name|container ID>
Example:
$ docker restart test-db
inspect
inspect is responsible for listing metadata about a running or stopped container. The command produces JSON output:
$ docker inspect [options] <container name|container ID>
Example:
$ docker -f '{{ .NetworkSettings.IPAddress }}' test-db
Options:
-f format the output using a "go" template.
rm
rm tells docker to remove the named container. You must stop the container first or you get an error.
$ docker rm <container name|container ID>
Example: Removing a single container
$ docker rm test-db
Example: Removing all containers
$ docker rm $(docker ps -aq)
Options:
-v Tells rm to remove any shared storage from the container as the container is deleted, but only if it is the last container that is using the shared storage.
Tip: If a container is removed without using the -v option and the container had shared volumes, the system may end up with dangling volumes.
images
images will list all the images currently installed on the system
$ docker images
rmi
rmi tells docker to remove the named image. All containers using this image must be removed first.
$ docker rmi <image>[:<tag>]
Example:
$ docker rmi mysql:5.5
Tag: If no tag is specified, it defaults to “latest”
Persistent Storage
Ownership
If the user that will run your application inside the container doesn’t exist on the host then you must use the UID instead of the name. For example if your user inside your database container is called mysql and has an id of 42 then you would run:
# chown 42 /var/dbfiles
SELinux
If you are running SELinux, you need to make sure the folder on the host machine has been setup correctly. Do this by making its context svirt_sandbox_file_t.
# chcon -t svirt_sandbox_file_t /var/dbfiles
Mounting Persistent Storage
To mount the host storage that you just setup, pass the -v option to the run command. The format is
$ docker run -v /var/dbfiles:/var/lib/mysql mysql
Port Forwarding
Port forwarding is setup by passing the -p option to the run command. The format is [
$ docker run -p 192.168.1.130:3306:3306 mysql