YouTip LogoYouTip

Docker Container Usage

A Docker container is a lightweight, portable, and self-contained software environment used to run applications. Docker containers encapsulate an application and all its dependencies (including libraries, configuration files, system tools, etc.) into a standardized package, enabling the application to run consistently anywhere. Containers are a form of operating system-level virtualization. They do not require running a full operating system, making them more efficient to start and run. ### Relationship Between Images and Containers * **Image**: A static template for a container, containing all the dependencies and files needed to run an application. Images are immutable. * **Container**: A running instance of an image, with its own filesystem, processes, network, etc., and is mutable. Containers start from images and remain mutable at runtime. !(#) * * * ## Docker Client The Docker client is a command-line tool for interacting with the Docker daemon. The Docker client is very simple; we can directly enter the `docker` command to see all the command options for the Docker client. @:~# docker !(#) You can learn more about the usage of a specific Docker command with the command **docker command --help**. For example, to view the specific usage of the **docker stats** command: @:~# docker stats --help !(#) Here are some commonly used Docker client commands: | **Command** | **Function** | **Example** | | --- | --- | --- | | `docker run` | Start a new container and run a command | `docker run -d ubuntu` | | `docker ps` | List currently running containers | `docker ps` | | `docker ps -a` | List all containers (including stopped ones) | `docker ps -a` | | `docker build` | Build an image from a Dockerfile | `docker build -t my-image .` | | `docker images` | List all locally stored images | `docker images` | | `docker pull` | Pull an image from a Docker repository | `docker pull ubuntu` | | `docker push` | Push an image to a Docker repository | `docker push my-image` | | `docker exec` | Execute a command in a running container | `docker exec -it container_name bash` | | `docker stop` | Stop one or more containers | `docker stop container_name` | | `docker start` | Start a stopped container | `docker start container_name` | | `docker restart` | Restart a container | `docker restart container_name` | | `docker rm` | Remove one or more containers | `docker rm container_name` | | `docker rmi` | Remove one or more images | `docker rmi my-image` | | `docker logs` | View the logs of a container | `docker logs container_name` | | `docker inspect` | Get detailed information about a container or image | `docker inspect container_name` | | `docker exec -it` | Enter the interactive terminal of a container | `docker exec -it container_name /bin/bash` | | `docker network ls` | List all Docker networks | `docker network ls` | | `docker volume ls` | List all Docker volumes | `docker volume ls` | | `docker-compose up` | Start a multi-container application (from a `docker-compose.yml` file) | `docker-compose up` | | `docker-compose down` | Stop and remove containers, networks, etc., started by `docker-compose` | `docker-compose down` | | `docker info` | Display detailed information about the Docker system | `docker info` | | `docker version` | Display version information for the Docker client and daemon | `docker version` | | `docker stats` | Display real-time resource usage of containers | `docker stats` | | `docker login` | Log in to a Docker repository | `docker login` | | `docker logout` | Log out of a Docker repository | `docker logout` | **Common Options Explained:** * **`-d`**: Run the container in the background (detached mode), e.g., `docker run -d ubuntu`. * **`-it`**: Run the container with an interactive terminal, e.g., `docker exec -it container_name bash`. * **`-t`**: Tag an image, e.g., `docker build -t my-image .`. * * * ## Container Usage ### Get an Image If we don't have the ubuntu image locally, we can use the `docker pull` command to download it: $ docker pull ubuntu ### Start a Container The following command uses the ubuntu image to start a container, with the parameter to enter the container in command-line mode: $ docker run -it ubuntu /bin/bash !(#) Parameter explanation: * **-i**: Interactive operation. * **-t**: Terminal. * **ubuntu**: The ubuntu image. * **/bin/bash**: The command placed after the image name. Here we want an interactive Shell, so we use `/bin/bash`. To exit the terminal, simply type **exit**: root@ed09e4490c57:/# exit !(#) ### Start a Stopped Container To view all containers, use the following command: $ docker ps -a Click the image to view it in full size: [!(#)](#) Use `docker start` to start a stopped container: $ docker start b750bbbcfd88 !(#) ### Run in Background In most scenarios, we want Docker services to run in the background. We can specify the container's run mode with the `-d` flag. $ docker run -itd --name ubuntu-test ubuntu /bin/bash Click the image to view it in full size: [!(#)](#) [!(#)](#) **Note:** When the `-d` parameter is added, the container will not be entered by default. To enter the container, you need to use the **docker exec** command (which will be introduced later). ### Stop a Container The command to stop a container is as follows: $ docker stop !(#) A stopped container can be restarted with `docker restart`: $ docker restart !(#) ### Enter a Container When a container is started with the **-d** parameter, it runs in the background. To enter the container, you can use the following commands: * **docker attach**: Allows you to interact with the container's standard input (stdin), output (stdout), and standard error (stderr). * **docker exec**: It is recommended to use the `docker exec` command because it will exit the container terminal without causing the container to stop. **The `attach` command** Example using the `docker attach` command: $ docker attach 1e560fca3906 [!(#)](#) **Note:** Exiting from this container will cause the container to stop. **The `exec` command** Example using the `docker exec` command: docker exec -it 243c32535da7 /bin/bash [!(#)](#) **Note:** Exiting from this container will not stop it. This is why **docker exec** is recommended. For more parameter details, use the `docker exec --help` command. ### Export and Import Containers **Export a Container** To export a local container, you can use the **docker export** command. $ docker export 1e560fca3906 > ubuntu.tar This exports the snapshot of container `1e560fca3906` to a local file `ubuntu.tar`. [!(#)](#) This will export the container snapshot to a local file. **Import a Container Snapshot** You can use `docker import` to import a container snapshot file back as an image. The following example imports the snapshot file `ubuntu.tar` into the image `test/ubuntu:v1`: $ cat docker/ubuntu.tar | docker import - test/ubuntu:v1 [!(#)](#) Additionally, you can import by specifying a URL or a directory, for example: $ docker import http://example.com/exampleimage.tgz example/imagerepo ### Delete a Container To delete a container, use the **docker rm** command: $ docker rm -f 1e560fca3906 [!(#)](#) The following command can clean up all stopped containers. $ docker container prune * * * ## Run a Web Application The containers we ran earlier didn't have any particular use. Next, let's try using Docker to build a web application. We will run a Python Flask application inside a Docker container to run a web application. @:~# docker pull training/webapp # Pull the image @:~# docker run -d -P training/webapp python app.py !(#) Parameter explanation: * **-d:** Run the container in the background. * **-P:** Randomly map the network ports used inside the container to the host machine. * * * ## View the WEB Application Container Use `docker ps` to view the containers we are running: @:~# docker ps CONTAINER ID IMAGE COMMAND ... PORTS d3d5e39ed9d3 training/webapp "python app.py" ... 0.0.0.0:32769->5000/tcp Here, there is additional port information. PORTS 0.0.0.0:32769->5000/tcp Docker has opened port 5000 (the default Python Flask port) and mapped it to host port 32769. Now we can access the web application through a browser. !(#) We can also use the `-p` parameter to set a different port: @:~$ docker run -d -p 5000:5000 training/webapp python app.py Use **docker ps** to view the running containers: @:~# docker ps CONTAINER ID IMAGE PORTS NAMES bf08b7f2cd89 training/webapp ... 0.0.0.0:5000->5000/tcp wizardly_chandrasekhar d3d5e39ed9d3 training/webapp ... 0.0.0.0:32769->5000/tcp xenodochial_hoov The internal port 5000 of the container is mapped to port 5000 on our local host. * * * ## Network Port Shortcut You can view the port mappings of a container with the **docker ps** command. Docker also provides another shortcut, **docker port**. Using **docker port**, you can see which host port a specific port of a given container (by ID or name) is mapped to. The web application container we created earlier has the ID **bf08b7f2cd89** and the name **wizardly_chandrasekhar**. I can use `docker port bf08b7f2cd89` or `docker port wizardly_chandrasekhar` to view the port mapping of the container. @:~$ docker port bf08b7f2cd89 5000/tcp -> 0.0.0.0:5000 @:~$ docker port wizardly_chandrasekhar 5000/tcp -> 0.0.0.0:5000 * * * ## View WEB Application Logs `docker logs ` can view the standard output of a container. @:~$ docker logs -f bf08b7f2cd89 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)192.168.239.1 - - [09/May/2016 16:30:37] "GET / HTTP/1.1" 200 -192.168.239.1 - - [09/May/2016 16:30:37] "GET /favicon.ico HTTP/1.1" 404 - **-f:** Makes **docker logs** output the container's standard output like **tail -f**. From the above, we can see the application is using port 5000 and we can view the application's access logs. * * * ## View WEB Application Container Processes We can also use `docker top` to view the processes running inside a container. @:~$ docker top wizardly_chandrasekhar UID PID PPID ... TIME CMD root 23245 23228 ... 00:00:00 python app.py * * * ## Inspect the WEB Application Use **docker inspect** to view the low-level information of Docker. It will return a JSON file recording the configuration and status information of the Docker container. @:~$ docker inspect wizardly_chandrasekhar [ { "Id": "bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85", "Created": "2018-09-17T01:41:26.174228707Z", "Path": "python", "Args": [ "app.py" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 23245, "ExitCode": 0, "Error": "", "StartedAt": "2018-09-17T01:41:26.494185806Z", "FinishedAt": "0001-01-01T00:00:00Z" },...... * * * ## Stop the WEB Application Container @:~$ docker stop wizardly_chandrasekhar wizardly_chandrasekhar * * * ## Restart the WEB Application Container For a stopped container, we can use the `docker start` command to start it. @:~$ docker start wizardly_chandrasekhar wizardly_chandrasekhar Use `docker ps -l` to query the last created container: # docker ps -l CONTAINER ID IMAGE PORTS NAMES bf08b7f2cd89 training/webapp ... 0.0.0.0:5000->5000/tcp wizardly_chandrasekhar For a running container, we can use the `docker restart` command to restart it. * * * ## Remove the WEB Application Container We can use the `docker rm` command to delete unwanted containers. @:~$ docker rm wizardly_chandrasekhar wizardly_chandrasekhar When deleting a container, the container must be in a stopped state; otherwise, the following error will occur: @:~$ docker rm wizardly_chandrasekhar Error response from daemon: You cannot remove a running container bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85. Stop the container before attempting removal or force remove
← Func Array Key LastPandas Json β†’