Essential Docker commands

Docker is the most popular container technology. It is designed to make it easier to create, deploy, and run applications by using containers. Docker behaves similar to a virtual machine hypervisor. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.

This tutorial lists essential Docker commands and how they are used.

Docker RUN Quick Reference

Taken directly from docker help, this table is a useful quick reference you can refer to as you go further with Docker.

Taken directly from docker help, this table is a useful quick reference you can refer to as you go further with Docker.

Option Description
-a, –attach=[] Attach to stdin, stdout or stderr.
-c, –cpu-shares=0 CPU shares (relative weight)
–cidfile=”” Write the container ID to the file
–cpuset=”” CPUs in which to allow execution (0-3, 0,1)
-d, –detach=false Detached mode: Run container in the background, print new container id
–dns=[] Set custom dns servers
–dns-search=[] Set custom dns search domains
-e, –env=[] Set environment variables
–entrypoint=”” Overwrite the default entrypoint of the image
–env-file=[] Read in a line delimited file of ENV variables
–expose=[] Expose a port from the container without publishing it to your host
-h, –hostname=”” Container host name
-i, –interactive=false Keep stdin open even if not attached
–link=[] Add link to another container (name:alias)
–lxc-conf=[] (lxc exec-driver only) Add custom lxc options –lxc-conf=”lxc.cgroup.cpuset.cpus = 0,1″
-m, –memory=”” Memory limit (format: , where unit = b, k, m or g)
–name=”” Assign a name to the container
–net=”bridge” Set the Network mode for the container
  ‘bridge’: creates a new network stack for the container on the docker bridge
  ‘none’: no networking for this container
  ‘container:’: reuses another container network stack
  ‘host’: use the host network stack inside the container. Note: the host mode gives the container full access to local system services such as D-bus and is therefore considered insecure.
-P, –publish-all=false Publish all exposed ports to the host interfaces
-p, –publish=[] Publish a container’s port to the host
  format: ip:hostPort:containerPort or ip::containerPort or hostPort:containerPort
  (use ‘docker port’ to see the actual mapping)
–privileged=false Give extended privileges to this container
–rm=false Automatically remove the container when it exits (incompatible with -d)
–sig-proxy=true Proxify received signals to the process (even in non-tty mode). SIGCHLD is not proxied.
-t, –tty=false Allocate a pseudo-tty
-u, –user=”” Username or UID
-v, –volume=[] Bind mount a volume (e.g., from the host: -v /host:/container, from docker: -v /container)
–volumes-from=[] Mount volumes from the specified container(s)
-w, –workdir=”” Working directory inside the container

Docker RUN Creating a Container instance

To create a running docker container from an image, you use the docker run command, like this:

$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
$ docker run -it ubuntu:latest /bin/bash

Here’s the detail about the options we’ve used in the RUN command:

  • The OPTIONS “-it” tell Docker to make the container interactive and provide a tty (i.e., attach a terminal)
  • The IMAGE is the name of the image that the container should start from. This is the “ubuntu:latest” image. Note the repository:tag format, which is used throughout docker. By default, the “latest” tag is used, but if we were to use “ubuntu:precise” we’d be using Ubuntu 12.04.
  • The COMMAND is the command that should run on the container when it starts. Here, “/bin/bash”, which starts a shell where you can log in

You can view your running containers using the “docker ps” command to see some information about the running container:

$ docker ps
CONTAINER ID  IMAGE  COMMAND  CREATED  STATUS PORTS  NAMES
4afa46473802 ubuntu:14.04  /bin/bash  17 hours ago  Up 39 seconds  test

This command will tell you:

  • The CONTAINER_ID, a unique identifier you can use to refer to the container in other commands (this is kind of like a process id in Linux)
  • The IMAGE that was used when the container started
  • The COMMAND used to start the container
  • The time the underlying image was CREATED
  • The uptime STATUS
  • The exposed ports
  • A human readable NAME that you can use in place of the ID

You can kill the docker process like this:

$ docker kill test
test

If you do docker ps again, you’ll see that the process has stopped.

Set docker container hostname when running

docker run --hostname name

Publish or map a container port to a port on the host.

docker run -it  -p  (container port):(host port)/protocol c--name <container-name> <imagename> <script>

Create your own local network

docker network create --subnet=172.18.0.0/16 <NETWORK NAME>

Create a docker with preferred local network

docker run --network <NETWORK NAME> --ip <IP ADDRESS> -itd --name <DOCKER NAME> --hostname <HOST NAME> <IMAGE NAME> /bin/bash

———————————-

– masterkenneth

Leave a Reply

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