How to use docker desktop start command to start Docker Desktop

DockerDockerBeginner
Practice Now

Introduction

In this lab, we will explore how to use the docker start command to manage the lifecycle of Docker containers. We will begin by understanding the fundamental purpose of docker start and how it differs from docker run.

Through hands-on exercises, you will learn to start stopped containers synchronously, start containers in detached mode, and specify a timeout for the start operation. By the end of this lab, you will be proficient in using docker start to resume your Docker containers efficiently.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/start("Start Container") docker/ContainerOperationsGroup -.-> docker/stop("Stop Container") docker/ContainerOperationsGroup -.-> docker/create("Create Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555148{{"How to use docker desktop start command to start Docker Desktop"}} docker/ps -.-> lab-555148{{"How to use docker desktop start command to start Docker Desktop"}} docker/start -.-> lab-555148{{"How to use docker desktop start command to start Docker Desktop"}} docker/stop -.-> lab-555148{{"How to use docker desktop start command to start Docker Desktop"}} docker/create -.-> lab-555148{{"How to use docker desktop start command to start Docker Desktop"}} docker/pull -.-> lab-555148{{"How to use docker desktop start command to start Docker Desktop"}} end

Understand the purpose of docker desktop start

In this step, we will understand the purpose of the docker start command. The docker start command is used to start one or more stopped containers. When you stop a container using docker stop, its state is saved, and you can resume it later using docker start. This is different from docker run, which creates a new container from an image and then starts it.

First, let's create a simple container that we can stop and then start. We will use the ubuntu image and run a command that keeps the container running for a short period.

docker run -d --name my-ubuntu ubuntu sleep 60

This command runs an Ubuntu container in detached mode (-d), names it my-ubuntu, and executes the sleep 60 command inside the container. The sleep 60 command will keep the container running for 60 seconds.

Now, let's check the status of the container using docker ps.

docker ps

You should see the my-ubuntu container listed with a status of Up.

Next, we will stop the container using the docker stop command.

docker stop my-ubuntu

This command sends a stop signal to the my-ubuntu container.

Now, let's check the status of the container again using docker ps.

docker ps

You should see that the my-ubuntu container is no longer listed in the output of docker ps (which shows only running containers). To see all containers, including stopped ones, you can use docker ps -a.

docker ps -a

You should now see my-ubuntu listed with a status of Exited. This confirms that the container has been stopped.

Finally, we will use the docker start command to start the stopped container.

docker start my-ubuntu

This command starts the my-ubuntu container.

Let's check the status of the container one last time using docker ps.

docker ps

You should see the my-ubuntu container listed again with a status of Up. This demonstrates that docker start successfully resumed the previously stopped container.

In summary, docker start is used to resume a stopped container, preserving its state, while docker run creates and starts a new container.

Start Docker Desktop synchronously

In this step, we will explore how to start a Docker container synchronously. When you start a container synchronously, the command waits for the container to finish its execution before returning control to your terminal. This is the default behavior when you run a command directly in a container without the -d (detached) flag.

Let's use a simple ubuntu container to demonstrate synchronous execution. We will run a command that prints a message and then exits.

First, ensure the ubuntu image is available. If not, pull it:

docker pull ubuntu

Now, run a container synchronously that prints "Hello from Ubuntu" and then exits.

docker run --rm ubuntu echo "Hello from Ubuntu"

Let's break down this command:

  • docker run: This is the command to create and run a new container.
  • --rm: This flag automatically removes the container when it exits. This is useful for temporary containers like this one.
  • ubuntu: This is the name of the image to use.
  • echo "Hello from Ubuntu": This is the command that will be executed inside the container.

When you run this command, you will see the output "Hello from Ubuntu" directly in your terminal. The command prompt will not return until the echo command inside the container has finished and the container has exited. This is synchronous execution.

Compare this to the detached mode we saw in the previous step, where the command returned immediately and the container ran in the background.

To further illustrate synchronous behavior, let's run a command that takes a bit longer.

docker run --rm ubuntu sleep 5

This command will run an Ubuntu container and execute the sleep 5 command, which pauses execution for 5 seconds. You will notice that your terminal will be blocked for 5 seconds before the command completes and the prompt returns. This is because the docker run command is waiting for the sleep 5 command inside the container to finish.

This synchronous mode is useful when you need to run a command in a container and wait for its output or completion before proceeding with further actions in your script or workflow.

Start Docker Desktop in detached mode

In this step, we will learn how to start a Docker container in detached mode. Detached mode means that the container runs in the background, and the Docker command immediately returns control to your terminal. This is useful for long-running services or applications that you don't need to interact with directly in the terminal.

To start a container in detached mode, you use the -d or --detach flag with the docker run or docker start command.

Let's start a simple Nginx web server container in detached mode. First, pull the nginx image if you don't have it locally.

docker pull nginx

Now, run the Nginx container in detached mode and map port 80 of the container to port 8080 on your host machine.

docker run -d --name my-nginx -p 8080:80 nginx

Let's break down this command:

  • docker run: Create and run a new container.
  • -d: Run the container in detached mode (in the background).
  • --name my-nginx: Give the container the name my-nginx.
  • -p 8080:80: Map port 80 inside the container to port 8080 on your host. This allows you to access the Nginx web server from your host machine's browser at http://localhost:8080.
  • nginx: The name of the image to use.

After running this command, you will see a long string of characters printed to your terminal. This is the container ID. The command prompt will return immediately, indicating that the container is running in the background.

To verify that the container is running, use the docker ps command.

docker ps

You should see the my-nginx container listed with a status of Up.

You can also test accessing the Nginx web server from your host machine using curl.

curl http://localhost:8080

You should see the HTML content of the default Nginx welcome page in your terminal. This confirms that the container is running and accessible via the mapped port.

To stop the detached container, you can use the docker stop command with the container name or ID.

docker stop my-nginx

After stopping the container, verify that it is no longer running using docker ps.

docker ps

The my-nginx container should not be listed in the output.

Starting containers in detached mode is essential for running services and applications that need to operate continuously without blocking your terminal.

Start Docker Desktop with a timeout

In this step, we will learn how to start a Docker container with a timeout. The docker start command has a --attach flag which allows you to attach to the container's STDOUT/STDERR and forward signals. When used with --attach, you can also specify a --timeout flag to set a timeout for the container to stop after the client disconnects. This is particularly useful for ensuring that a container doesn't keep running indefinitely if the client that started it is terminated.

First, let's create a container that we can start with a timeout. We will use the ubuntu image and run a command that keeps the container running.

docker create --name my-timeout-ubuntu ubuntu sleep 60

This command creates a container named my-timeout-ubuntu from the ubuntu image and sets the command to sleep 60. Note that we used docker create here, which creates the container but does not start it.

Now, we will start this container using docker start with the --attach and --timeout flags. We will set the timeout to 5 seconds.

docker start --attach --timeout 5 my-timeout-ubuntu

Let's break down this command:

  • docker start: Start a stopped container.
  • --attach: Attach to the container's STDOUT/STDERR and forward signals.
  • --timeout 5: Set a timeout of 5 seconds. If the client (your terminal in this case) disconnects, Docker will wait for 5 seconds for the container to stop gracefully. If it doesn't stop within 5 seconds, Docker will send a SIGKILL signal to force stop it.
  • my-timeout-ubuntu: The name of the container to start.

When you run this command, you will be attached to the container's output. Since the container is just running sleep 60, you won't see any output immediately.

Now, let's simulate a client disconnection. You can do this by pressing Ctrl+C in your terminal. This will detach you from the container.

After pressing Ctrl+C, Docker will start the timeout process. It will wait for 5 seconds for the my-timeout-ubuntu container to stop. Since the container is running sleep 60, it will not stop within 5 seconds, and Docker will force stop it.

To verify that the container has stopped, use docker ps -a.

docker ps -a

You should see my-timeout-ubuntu listed with a status of Exited. This confirms that the timeout mechanism worked and the container was stopped after the client disconnected.

Using the --timeout flag with docker start --attach is a good practice for managing the lifecycle of containers that are started with a client connection, ensuring they are cleaned up properly even if the client terminates unexpectedly.

Summary

In this lab, we learned the fundamental purpose of the docker start command, which is to resume a stopped container while preserving its state. We differentiated it from docker run, which creates and starts a new container. Through practical steps, we demonstrated how to create a container, stop it using docker stop, verify its stopped state using docker ps -a, and finally restart it using docker start, confirming its running status with docker ps.

We also explored different ways to start Docker Desktop using the docker desktop start command, including starting it synchronously, in detached mode, and with a specified timeout. These options provide flexibility in controlling the startup behavior of Docker Desktop based on specific needs and workflows.