DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Have You Heard About Cloud Native Buildpacks?
  • Setting Up Local Kafka Container for Spring Boot Application
  • Docker Image Building Best Practices
  • An Introduction to BentoML: A Unified AI Application Framework

Trending

  • Performance Optimization Techniques for Snowflake on AWS
  • Concourse CI/CD Pipeline: Webhook Triggers
  • Medallion Architecture: Efficient Batch and Stream Processing Data Pipelines With Azure Databricks and Delta Lake
  • Why High-Performance AI/ML Is Essential in Modern Cybersecurity
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Docker Commands You Should Know as a Beginner

Docker Commands You Should Know as a Beginner

Have you started using docker for the very first time and getting confused between a plethora of commands. Do not worry, this blog will emphasize on the Docker commands you should know as a beginner.

By 
Mudit Chhabra user avatar
Mudit Chhabra
·
Aug. 18, 22 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
7.9K Views

Join the DZone community and get the full member experience.

Join For Free

Before going into the functionality and features of Docker, we need to understand why we need Docker.

DevOps meme

If you have been around this industry for quite some time now, then you would have come across this meme about the introduction of Docker. Well, this meme is apparently funny but somehow turns out to be a factual one.

Life Before Docker

Before the introduction of Docker, shipping applications to the intended users were not only a complex task but tedious as well. The artifacts like zip files, wheel files, or even JAR files were shipped to the user which was then deployed on their machines. Now, you will wonder if this method of shipping and deploying applications was successful or not. 

The answer lies in the after-effects of the deployment of the applications. Once an application or software is deployed on the user's machine, it may not work as intended. Two of the main reasons why the applications do not perform the same way in all the machines are:

  1. Applications version discrepancy between different machines
  2. Inconsistent environment variables across machines

Docker helped in solving both the problems by packaging software in a way that the versions for all the parts of the software are defined in a file called Dockerfile.

Dockerfile

Dockerfile is a text-based document that contains all the information about the software dependencies. Dockerfile is executed on the user's system and with the help of commands listed in the Dockerfile the software is installed on the user's system.

Usually the Dockerfile is the only thing shipped to the user

Python
 
# file.py
print('Hello All')

The following is a sample Dockerfile to understand the concept.

Dockerfile
 
FROM ubuntu:latest
COPY . /loc
RUN make /loc
CMD python /loc/file.py

Let us see how the above Dockerfile works.

1. FROM ubuntu:latest: This creates a layer of the ubuntu operating system on which the rest of the commands from the Dockerfile can be executed. As soon as this line is executed, the ubuntu OS is downloaded from the Docker Hub.

2. COPY . /loc: This copies all the files from the current directory to the /loc directory.

3. RUN make /loc: Once we have our ubuntu OS installed, we can use make command to compile the files in /loc directory.

4. CMD python /loc/file.py: Using the CMD command, the python file file.py is executed on the user's machine.

Layers in Dockerfile

Please note, this is a basic Dockerfile, which is far away from from the Dockerfile used for a real software release.

How To Run a Dockerfile?

Once the Dockerfile is shipped to the user, the Dockerfile should be executed using some special commands. Let us see the process of executing a Dockerfile.

Building the Dockerfile

docker build -t mudit111/docker .

Basically, build command is used to create a Docker image from a Dockerfile. The syntax of the command uses -t to name your newly created Docker image. The . used at the end of the command tells the compiler that the Dockerfile is present in the current directory only.

Now, if you try to find out the Docker image in the current directory you will not find anything since the Docker image is not stored as a single file. The storage of a Docker image is taken care of by the system. But what if you need to look at the images present in the system at a point in time? We will see that in our next section.

Listing the Docker images

docker image

Using the above command you can easily list down all the Docker images currently in the system. The listing of the Docker images would include the following details:

  • REPOSITORY
  • TAG
  • IMAGE_ID
  • CREATED
  • SIZE

docker images
These Docker images can also be pushed to the Docker Hub for other users to download your project easily. Docker Hub is similar to GitHub. Just like GitHub stores your code, Docker Hub stores Docker images.

Running a Docker Image

docker run mudit111/docker

 Running the mudit111/docker Docker image, you will get the output as:

docker run

As soon as a Docker image is run, a docker container is created. The Docker container stays in a running state until the completion of the Docker image. Once the Docker image is complete, the Docker container stops automatically.

Listing the Docker Containers

docker ps

This command helps in viewing the running containers. But, this will list down only those containers that are still in a running state. The containers which were created and are now stopped will not be shown using this command. For listing all the containers, including the ones which are currently not in running state, you need to use -all at the end of the existing command.

docker pe

As our container is currently not running, the docker ps command will not show anything. But, adding -all at the end of the command will show all the stopped as well as running containers.

Pushing a Docker Image to Docker Hub

docker push mudit111/docker 

Once the Docker image has been created, you can easily publish the image to Docker Hub using docker push command. Pushing the Docker image to Docker Hub enables users around the globe to access your Docker image on their machine. 

Before you can push your Docker image to Docker Hub, you should have a valid Docker Hub account.

docker

Similarly, you can also pull any public Docker image from Docker Hub

Pulling a Docker Image From Docker Hub

docker pull mudit111/docker

By using the docker pull command, you can get any Docker image into your system. Once the Docker image is downloaded to your system, you can use docker run to run the image.

docker command

Once you are through with these commands, you can try to make a sample Dockerfile and upload it to the Docker Hub. You can, then, ask anyone to download your Docker image from Docker Hub and try to run the image.

If you want to try using the Docker commands interactively, you can signup on Docker Playground

Glad you reached this far in the blog. If you have any questions you can ask them in the comments section. Thanks!

application Docker (software)

Opinions expressed by DZone contributors are their own.

Related

  • Have You Heard About Cloud Native Buildpacks?
  • Setting Up Local Kafka Container for Spring Boot Application
  • Docker Image Building Best Practices
  • An Introduction to BentoML: A Unified AI Application Framework

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!