Skip to content

Instantly share code, notes, and snippets.

@maheensaleh
Last active October 8, 2020 17:06
Show Gist options
  • Save maheensaleh/ee563e779ea039a22b6a38004c1ce6e5 to your computer and use it in GitHub Desktop.
Save maheensaleh/ee563e779ea039a22b6a38004c1ce6e5 to your computer and use it in GitHub Desktop.
Docker Basic Commands

Basic Docker Commands

Below are some basic docker commands which I learnt from PIAIC Docker course by Amir Pinger. Made this gist to have all the basic commands listed together

Download docker image from docker hub

For official images : docker image pull image_name:tag For unofficial images : docker image pull username/imagename:tag

  • not necessary to write image, can also do docker pull direct
  • if no tag given then latest tag considered by default.

List Docker Images:

docker image ls or docker images

Remove/Delete Images :

docker image rm imagename:tag

  • Images can be removed only when they have no containers in stop or running state.

Create and Run Container:

docker run -it username/imagename sh

  • run : to run the container after creating it

  • -it : to bring the running container in interactive mode i.e. to start its terminal on our command line.

  • sh : type of terminal for interactive mode. Can also use bash instead which is another kind of terminal.

  • If no name provided for container then docker will give a default name to it

  • After running this command, main terminal will switch to the running containers shell.

  • To exit this interactive terminal and go back to the main terminal, use exit.

Note :

  • If the image doesn't exist locally, then it will be downloaded first, then its container is created.

  • exit will also stop that container.

  • For exiting the interactive terminal without stopping the container, use Clt+p+q.Once used this

  • later if you use exit on the shell of this running container, the interactive shell will close without stopping the container.

List Running Containers:

docker container ls or docker ps

List All Containers:

docker container ls -a or docker ps -a

will list all containers in any state ( running or stop )

Start interactive terminal for a running container :

docker exec -it container_name sh or docker exec -it container_id sh

Stop Container:

docker stop container_name or docker stop container_id If container stop is successful, the container_name or container_id will be displayed.

Starting a stopped container:

docker start container_name If container start is successful, the container_name or container_id will be displayed.

Removing Container:

docker container rm container_name or docker container rm container_id

Note : container must be in stop state inorder to delete/remove it.

Custom naming container:

docker container run -it --name cont_name username/imagename sh

Publish the container on a port:

docker container run -d -p local_host_port:tcp_port username/image_name

You can see it from browser at localhost:port

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment