Docker Basics 101

Building Docker Image

Once you create your Dockerfile for your image, you need to build it.  You do that by running the following command from the directory that contains the Dockerfile.

 docker build -t <image_name> .

Change “image_name” to be the name you want to give this image.  If you need to update your Dockerfile, you can simply rerun this command again to update your image.

Creating a Docker Container

With your image created your ready to start it.  Use this command to create the container:

docker run -t -i <image_name>

You can use this command multiple times, to create multiple instances of different or the same images.

Mounting a Host Directory in a Container

Docker Containers do not lend themselves to NFS as easily as one would like. However you can mount directories from the host system into your container.  When starting your image add the follow:

docker run -t -i -v /host/src/dir:/container/dest/dir <image_name>

You may add as many “-v <host dir>:<container dir>” as needed.  This will map “/host/src/dir” to the directory “/container/dest/dir” inside the container.

Naming a Docker Image

You may want an easier way to pick the container you want to Inspect or review.  You can give your containers a name, instead of having to use their Container ID.  To do this use the following:

docker run -t -i -v /host/src/dir:/container/dest/dir --name <Container Name> <image_name>

You can then use <Container Name> in replacement of the Container ID.

Running Docker Image in the Background

You probably do not want ot have to waste a session running docker int he foreground.  Instead run the container in the back ground add “-d” to the command line.

docker run -t -i -d -v /host/src/dir:/container/dest/dir --name <Container Name> <image_name>

Viewing the Running Containers

You have started multiple containers in Docker, and you need to know what is running.

docker ps

This will give you a listing of all running images:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
dd27c3df3d75        docker-image        "/usr/bin/supervisord"   39 seconds ago      Up 39 seconds       22/tcp              slave1

Viewing an Container Details

Sometimes you want to get specific information about an image.  This could be many things, the IP address is very common.  Use the Container ID from the “docker ps” command for the image you need additional information on, in the following command:

docker inspect <Container ID>

This will produce the following information:

[
    {
        "Id": "dd27c3df3d758d52fbbf5779e11bff670d8b25bd15975371f8314447d2f82301",
        "Created": "2016-04-26T20:29:03.197112472Z",
        "Path": "/usr/bin/supervisord",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 3404,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2016-04-26T20:29:03.766274435Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:6a3c4a49d51c50589b9d8e1ce3e07c23cb9dbe05bcf7fbb96b8f2a87593f2332",
        "ResolvConfPath": "/var/lib/docker/containers/dd27c3df3d758d52fbbf5779e11bff670d8b25bd15975371f8314447d2f82301/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/dd27c3df3d758d52fbbf5779e11bff670d8b25bd15975371f8314447d2f82301/hostname",
        "HostsPath": "/var/lib/docker/containers/dd27c3df3d758d52fbbf5779e11bff670d8b25bd15975371f8314447d2f82301/hosts",
        "LogPath": "/var/lib/docker/containers/dd27c3df3d758d52fbbf5779e11bff670d8b25bd15975371f8314447d2f82301/dd27c3df3d758d52fbbf5779e11bff670d8b25bd15975371f8314447d2f82301-json.log",
        "Name": "/slave1",
        "RestartCount": 0,
        "Driver": "devicemapper",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/mnt/nas:/mnt/nas"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "StorageOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "BlkioIOps": 0,
            "BlkioBps": 0,
            "SandboxSize": 0
        },
        "GraphDriver": {
            "Name": "devicemapper",
            "Data": {
                "DeviceId": "173",
                "DeviceName": "docker-253:0-203845648-9730e96e91695d973493214b8fe0d3eb14b0d30d91b9971aa1bed83f457726ba",
                "DeviceSize": "10737418240"
            }
        },
        "Mounts": [
            {
                "Source": "/mnt/nas",
                "Destination": "/mnt/nas",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "dd27c3df3d75",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "22/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/usr/bin/supervisord"
            ],
            "Image": "docker-image",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "9e347e035c2a0f7e7516404f4acc90a6d59ae40e6829efc4a4b075f88228361a",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "22/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/9e347e035c2a",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "a1b438922cb1078dba7f124c1cdd18461704e0898c8d6f836c850d36efc06455",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "92e12e2e71d2ca523e274186af0842b44dc55053baa6d34f6e356e29a5b3075a",
                    "EndpointID": "a1b438922cb1078dba7f124c1cdd18461704e0898c8d6f836c850d36efc06455",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02"
                }
            }
        }
    }
]

Stopping a Docker Container

At some point your probably going to need to stop your Docker Container.  You can do this with the following command:

docker stop <Container ID>

Starting a Docker Container

So you stopped a container, and now you need to restart it.  Use this simple command:

docker start <Conatiner ID>

Removing a Docker Container

Again your going to want to remove containers you have created at some point.  To do this use the following command:

docker rm <Container ID>doc

Additional information Check here:

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

Ghostprogrammer
  • Ghostprogrammer
  • I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: