3. Docker Run

  Docker

< 2 Docker Introduction and Commands | 4 Docker Images >

14. Docker Run

https://www.udemy.com/learn-docker/learn/lecture/7894016#overview

https://docs.docker.com/engine/reference/commandline/run/

Tags <image>:<tag>

<image>:<tag>
  • Specify a version of an image by running the image name followed by a colon and then the version number.
  • If no tag is applied, the default tag is ‘latest’

Links

-i -it Container Input stdin

docker run -it

-i Maps the standard input (keyboard) to the container

-t ‘psudo tty’ allows users to see the standard output from a process.  (read text it displays)

-p <HostPort>:<ContainerPort> Map Ports

docker run -p 80:5000 <image>
  • Maps the host port to the container port.
  • Allows you to run multiple containers of the same application and access them via their assigned port

-v <HostPath>:<ContainerPath> Volume mapping

Used to store and access persistent data.  Data not mapped to a persistent drive will be lost when the container stops.

docker run -v /opt/datadir:/var/lib/mysql mysql

 

docker inspect

While not a ‘run’ command, the ‘inspect’ command will display a container’s [service] definition information in a JSON format.

$ docker inspect 30ff
[
    {
        "Id": "30ff2dcf37fbebcd6765c0f59252368fc5cf1cadf4a64b8d9cae90a0822c55a9",
        "Created": "2020-04-15T18:53:51.286558212Z",
        "Path": "sleep",
        "Args": [
            "20"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 3499,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-04-15T18:53:51.814429508Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee",
...

Container Logs

docker logs <container-name>

 

15. Demo – Advanced Run Commands

https://www.udemy.com/course/learn-docker/learn/lecture/7894024#overview

Running bash commands

docker run centos cat /etc/*eleas*
cat: /etc/lsb-release: No such file or directory
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

Run a different version using tags from Docker Hub.

docker run centos:7 cat /etc/*eleas*
Unable to find image 'centos:7' locally
7: Pulling from library/centos
ab5ef0e58194: Pull complete 
Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c
Status: Downloaded newer image for centos:7
cat: /etc/lsb-release: No such file or directory
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

Run in -d Detached mode

This will return you to the current session command prompt while keeping the container alive.

docker run -d centos sleep 1000
6ec5790987e63dd81904daab4c920f456fadc7a465292e069ee5c15ca27405c8

Forgetting the -d may require you to open a different shell then

docker stop <container_id>

16 Labs

17 Reviews

18 LinkedIn

 

LEAVE A COMMENT