DEV Community

Isuru
Isuru

Posted on

Essential Docker Commands and Best Practices for Developers

1. Building a Docker Image
The docker build command creates a Docker image from a Dockerfile, packaging your application and its environment into a portable image. Use the following command to build an image:

docker build -t myapp .

This command will name the image myapp. You can check all created images by running:

docker images

2. Running a Docker Container
Once your image is built, you can run it inside a container. To create and run a container from the image, use the command:

docker run --name myapp-container -p 4000:4000 myapp

This command creates a container named myapp-container and maps port 4000 of your host machine to port 4000 inside the container.

3. Viewing Running Containers
To see all currently running containers, use the command:

docker ps

This will list all active containers along with their status and port mappings.

4. Stopping a Docker Container
If you need to stop a running container, you can do so with:

docker stop myapp-container

This will gracefully stop the myapp-container.

5. Restarting a Docker Container
To restart a previously stopped container, simply run:

docker start myapp-container

6. Removing a Docker Container
When you're done with a container and want to remove it, use the following command:

docker container rm myapp-container

This will completely remove the container from your system.

7. Removing a Docker Image
If you no longer need an image, you can remove it using:

docker image rm myapp

8. Cleaning Up Docker Resources
To remove all stopped containers, unused images, and volumes, use the docker system prune command. To remove everything, including all images and volumes, use:

docker system prune -a

This is a handy way to free up space.

9. Versioning Docker Images
Versioning is crucial when dealing with different stages of development and production. You can tag and version Docker images as follows:

docker build -t myapp:v1 .

This will tag the image as v1. To run the versioned image:

docker run --name myapp-container -p 4000:4000 myapp:v1

10. Running a Container with Nodemon for File Changes
For Node.js developers, using nodemon inside a Docker container can enable automatic reloading of your app on file changes. To achieve this, mount the app directory as a volume with the docker run command:

docker run --name myapp_c_nodemon -p 5000:4000 --rm -v /d/docker/docker-crash/api:/app -v /app/node_modules myapp:nodemon

Note: If you're using Git Bash on Windows, use this path format:

docker run --name myapp_c_nodemon -p 5000:4000 --rm -v //d/docker/docker-crash/api:/app -v /app/node_modules myapp:nodemon

11. Using Docker Compose for Multi-Container Applications
When you have multiple services that need to run together, Docker Compose simplifies the process. Here’s an example docker-compose.yml file:

version: '3.8'
services:
  api:
    build: ./api
    container_name: api_c
    ports:
      - "5000:4000"
    volumes:
      - ./api:/app
      - /app/node_modules
Enter fullscreen mode Exit fullscreen mode

12. Running Docker Compose
To start up all services defined in your docker-compose.yml file, simply run:

docker-compose up

This command will build images (if necessary) and start all the services in the specified configuration.

13. Stopping and Removing Docker Compose Containers
When you're done, you can stop and remove the services with:

docker-compose down

To also remove volumes and images:

docker-compose down --rmi all -v

Conclusion
By mastering these Docker commands, you can efficiently manage your images and containers, making your development and production workflow much smoother. Whether you're building, running, or versioning containers, these commands will serve as your essential toolbox for all Docker tasks.

Top comments (0)