Docker Desktop is a comprehensive tool that simplifies the process of developing, shipping, and running containerized applications. It provides an easy-to-use interface for managing Docker containers and images on your local machine. Whether you're a beginner or an intermediate developer, Docker Desktop offers features that cater to various development needs.
In this tutorial, we will explore how to use Docker Desktop for local development and testing. We'll cover the installation process, basic operations, and practical examples to help you get started with containerization using Docker Desktop.
Docker Desktop combines Docker Engine, Docker CLI, and a graphical user interface (GUI) into a single application. This makes it easier to manage containers and images without needing to use command-line tools exclusively. Here are some key features of Docker Desktop:
To get started with Docker Desktop, you need to install it on your local machine. Follow these steps based on your operating system:
.dmg file and drag Docker Desktop to the Applications folder..exe file and follow the installation wizard.After installing Docker Desktop, you can start using it to manage containers and images. Here are some basic operations:
To run a container, use the docker run command followed by the image name. For example, to run an Nginx web server:
$ docker run -d -p 80:80 nginx
This command will download the Nginx image from Docker Hub and start a container in detached mode (-d). The -p 80:80 option maps port 80 of the host to port 80 of the container.
To list all running containers, use the docker ps command:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES abc123 nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp ecstatic_hopper
To list all containers (including stopped ones), use the docker ps -a command:
$ docker ps -a
To stop a running container, use the docker stop command followed by the container ID or name:
$ docker stop abc123
To remove a stopped container, use the docker rm command followed by the container ID or name:
$ docker rm abc123
Let's create a simple Dockerfile to build an image and run it using Docker Desktop.
$ mkdir my-docker-app$ cd my-docker-app
Dockerfile with the following content:1FROM node:1423WORKDIR /usr/src/app45COPY package*.json ./67RUN npm install89COPY . .1011EXPOSE 30001213CMD ["node", "server.js"]
package.json and a server.js file:1{2"name": "my-docker-app",3"version": "1.0.0",4"main": "server.js",5"scripts": {6"start": "node server.js"7},8"dependencies": {9"express": "^4.17.1"10}11}
1const express = require('express');2const app = express();3const port = 3000;45app.get('/', (req, res) => {6res.send('Hello World!');7});89app.listen(port, () => {10console.log(`App listening at http://localhost:${port}`);11});
$ docker build -t my-docker-app .
$ docker run -d -p 3000:3000 my-docker-app
http://localhost:3000. You should see "Hello World!" displayed.In this tutorial, we covered the basics of using Docker Desktop for local development and testing. If you're interested in managing multiple machines with Docker, you might want to explore Docker Context, which allows you to manage multiple Docker environments.
By mastering Docker Desktop, you'll be well-equipped to containerize your applications and streamline your development workflow. Happy coding!