Docker – Commands Reference

1.General Docker command

List info about the Docker Client:

$ docker version

Get docker command help:

—- List all commands:

$ docker –help

—- For a specific command:

$ docker <command> –help

— List networks:

$ docker network ls

 

2.Containers

— List containers:

—- List containers that are up:

$ docker ps

—- List the last started container:

$ docker ps -l

—- List all the containers:

$ docker ps -a

Start a container:

$ docker start <container_id>

— Stop containers:

—- Stop a container:

$ docker stop <container_id>

—- Stop all containers:

$ docker stop $(docker ps -a -q)

Restart a container:

$ docker restart <container_id>

— Remove a container:

$ docker rm <container_id>

— Delete all containers:

$ docker rm $(docker ps -a -q)

— Run a container(creates and starts the container):

—- Run a container:

$ docker run <image_name> <command>

For ex:

$ docker run ubuntu /bin/echo ‘Hello world’

—- Run a container in interactive mode:

$ docker run -t -i <image_name> <command>

This will launch a Bash shell inside our container.

—- Run a container in daemonized mode:

$ docker run -d <image_name> <command>

This will run the container and put it in the background, to daemonize it.

Commit (save) a container state

$ docker commit <container> <some_name>

— Get container info:

$ docker inspect <container_id>

Returns a JSON document with configuration and status info.

We can also narrow down the info we want to return by requesting a specific element.

For example, this gives the IP address of the container:

$ docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ <container_id>

Return container standard output:

$ docker logs <container_id>

Acts like tail -f:

$ docker logs -f <container_id>

Examine processes inside a container:

$ docker top <container_name>

— Show container size:

$ docker ps -s

 

3.Images

— Create an image:

—- Use Dockerfile:

$ docker build -t <username/image_name:tag> .

The -t flag is to identify the new docker image as belonging to <username/image_name:tag>.

. is to indicate use the Dockerfile in the current directory.

—- Use commit command:

$ docker commit -m “<message>” -a “<author>” <container_id> <docker_hub_username>/<image_name>:<tag>

For ex: $ docker commit -m “Added json gem” -a “Kate Smith” 0b2616b0e5a8 ouruser/sinatra:v2

— List images:

$ docker images

— Remove an image:

$ docker rmi <image_id>

— Delete all images:

$ docker rmi $(docker images -q)

Pull an image

$ docker pull <image_name>

Pulls an image from docker hub/specified host.

— Build an image

$ docker build -t <host/img_name:tag> .

Builds an image with specified hostname as prefix.

— Push an image

$ docker push <username/image_name>

Pushes the docker image to the specified host/docker hub.

 

4.docker-compose

docker-compose build:

Build the containers that the web app and db will live in, and link them up.

— docker-compose run:

Run one-off commands for services. For example, to see environment variables are available to the web service:

$ docker-compose run web env

— docker-compose up:

Build, link, and start services.

 

5.docker-machine

–Create a docker machine:

$ docker-machine create –driver virtualbox <docker_machine_name>

–List docker machines:

$ docker-machine ls

–Restart docker machine:

$ docker-machine restart <docker_machine_name>

–Stop docker machine:

$ docker-machine stop <docker_machine_name>

–Connect to a docker machine:

$docker-machine ssh <docker_machine_name>

 

6.Useful commands

Check iptables:

$ sudo iptables -t nat -L -n

— Find the PID of the process:

$ ps aux | grep <process_name>

— Kill a process:

$ kill <pid>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s