Docker Run Sudo

2/13/2022by admin

Containers are run using docker run command. Make Docker Run Without Sudo Command. Usage: See docker run documentation for details. Follow the example below to run the L4T-base container: Visit the L4T-base container page for explanation on the above docker run command. Congratulations on running your first container on Jetson! Sudo docker run -d -P testsite. Now, use the docker ps command to determine the port activated and then use curl to inspect the sample content. $ sudo docker ps. The command for running a container in the background is: docker container run -d dockerimage For our example, the command is: docker container run -d e98b6ec72f51. The output you receive will be similar to the one you see in the image above. The container will run the process and then stop.

  1. Docker Run Sudo Or Not
  2. Docker Run Without Sudo
  3. Docker Run Name
  4. Docker Run Example

Apr 01, 2019

3min Read

Just like Linux was an accidental revolution by Linus Torvalds, Docker was by Solomon Hykes. Solomon was trying to figure out a solution for programmers to provide a code that runs the same both on their development as well as the production environment and this resulted in the creation of Docker Containers. In this tutorial, we’ll show you how you can start a Docker container on your VPS, along with some extra tips and commands.

Today Docker is the most popular software container platform.

Docker Explained

At a very basic level it solves the issue of an application running on one platform but not on others. Although present in the entire software development lifecycle, Docker Containers main use is in deployment.

What are Docker Containers

Docker Containers are running instances of Docker images. Running an Image creates a Docker container. Images provide a template that can be used for the creation of containers. They contain information about what’s required to create containers. Images can either be stored locally or remotely. If you need help installing Docker, check out our tutorials for CentOS 7 or Ubuntu 18.04.

How to Start a Docker Container

Remember, before starting, you need to access your VPS server with SSH. Check out our PuTTY tutorial if you’re having trouble.

To list all Dockers images on your system you can use the command:

If you want to display additional information enter the following command in the command line:

We don’t have any Docker Images on our system, so let’s pull an image first. To do that, first go to Docker hub. Here you can find hundreds of Docker Images. We’ll pull an Ubuntu image. You can browse each image’s page to see more details about the Image:

We can pull the image with the command:

You can replace <image name> with hundreds of images found on Docker Hub like CentOS, MySQL, mariaDB, Python, etc.

Using the -q option will list only numeric IDs of Images available on your system.

-f is the filter flag. If we wanted to list all images that are not dangling – tagged or referenced by a container – we would use the command:

Now that we know how to pull and locate an image to start a Docker container, we can get to work.

Next we see how to run an Image, by running an Image we actually create a container out of that image. Lets run our ubuntu image. To start a Docker container use the command:

We’ll run the Ubuntu image. So the command will be:

The container is created, but not started

To start the container we use a command like this:

Here –name MyContainer is simply how we want to name the running process, while -it ubuntu bash, names which container we’re running.

Nowe we can open another terminal window, SSH into the server and run the command:

Open another terminal and issue following command.

We can now see that the container named MyContainer is now running.

Docker Run Sudo Or Not

To stop the container issue following command:

If we wish to see the top process of a container we can run the command:

It would look like this:

To see stats of a container like its CPU utilization, memory usage, etc.:

Lastly, if we wish to kill a Docker container:

That’s all there is to it! You’re ready to start a Docker container and put it to use!

Wrap Up

Docker is an incredibly useful tool for any developer. The ability to seamlessly test, deploy and develop applications is a utility that can speed up workflow exponentially. In this tutorial, we show how to start a Docker container, along with some other commands that will definitely come in handy.

To learn more we suggest checking out our other tutorials, or Docker’s official documentation!

Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Tutorial

Introduction

Docker is a containerization tool that helps developers create and manage portable, consistent Linux containers.

When developing or deploying containers you’ll often need to look inside a running container to inspect its current state or debug a problem. To this end, Docker provides the docker exec command to run programs in containers that are already running.

In this tutorial we will learn about the docker exec command and how to use it to run commands and get an interactive shell in a running Docker container.

Prerequisites

This tutorial assumes you already have Docker installed, and your user has permission to run docker. If you need to run docker as the root user, please remember to prepend sudo to the commands in this tutorial.

For more information on using Docker without sudo access, please see the Executing the Docker Command Without Sudo section of our How To Install Docker tutorial.

Starting a Test Container

To use the docker exec command, you will need a running Docker container. If you don’t already have a container, start a test container with the following docker run command:

This command creates a new Docker container from the official alpine image. This is a popular Linux container image that uses Alpine Linux, a lightweight, minimal Linux distribution.

We use the -d flag to detach the container from our terminal and run it in the background. --name container-name will name the container container-name. You could choose any name you like here, or leave this off entirely to have Docker automatically generate a unique name for the new container.

Next we have alpine, which specifies the image we want to use for the container.

And finally we have watch 'date >> /var/log/date.log'. This is the command we want to run in the container. watch will repeatedly run the command you give it, every two seconds by default. The command that watch will run in this case is date >> /var/log/date.log. date prints the current date and time, like this:

The >> /var/log/date.log portion of the command redirects the output from date and appends it to the file /var/log/date.log. Every two seconds a new line will be appended to the file, and after a few seconds it will look something like this:

Docker run sudo test

In the next step we’ll learn how to find the names of Docker containers. This will be useful if you already have a container you’re targeting, but you’re not sure what its name is.

Finding the Name of a Docker Container

We’ll need to provide docker exec with the name (or container ID) of the container we want to work with. We can find this information using the docker ps command:

This command lists all of the Docker containers running on the server, and provides some high-level information about them:

In this example, the container ID and name are highlighted. You may use either to tell docker exec which container to use.

If you’d like to rename your container, use the docker rename command:

Next, we’ll run through several examples of using docker exec to execute commands in a running Docker container.

Running an Interactive Shell in a Docker Container

If you need to start an interactive shell inside a Docker Container, perhaps to explore the filesystem or debug running processes, use docker exec with the -i and -t flags.

The -i flag keeps input open to the container, and the -t flag creates a pseudo-terminal that the shell can attach to. These flags can be combined like this:

This will run the sh shell in the specified container, giving you a basic shell prompt. To exit back out of the container, type exit then press ENTER:

If your container image includes a more advanced shell such as bash, you could replace sh with bash above.

Running a Non-interactive Command in a Docker Container

If you need to run a command inside a running Docker container, but don’t need any interactivity, use the docker exec command without any flags:

This command will run tail /var/log/date.log on the container-name container, and output the results. By default the tail command will print out the last ten lines of a file. If you’re running the demo container we set up in the first section, you will see something like this:

This is essentially the same as opening up an interactive shell for the Docker container (as done in the previous step with docker exec -itcontainer-name sh) and then running the tail /var/log/date.log command. However, rather than opening up a shell, running the command, and then closing the shell, this command returns that same output in a single command and without opening up a pseudo-terminal.

Running Commands in an Alternate Directory in a Docker Container

To run a command in a certain directory of your container, use the --workdir flag to specify the directory:

This example command sets the /tmp directory as the working directory, then runs the pwd command, which prints out the present working directory:

The pwd command has confirmed that the working directory is /tmp.

Running Commands as a Different User in a Docker Container

To run a command as a different user inside your container, add the --user flag:

This will use the guest user to run the whoami command in the container. The whoami command prints out the current user’s username:

Docker Run Without Sudo

The whoami command confirms that the container’s current user is guest.

Passing Environment Variables into a Docker Container

Sometimes you need to pass environment variables into a container along with the command to run. The -e flag lets you specify an environment variable:

This command sets the TEST environment variable to equal sammy, then runs the env command inside the container. The env command then prints out all the environment variables:

The TEST variable is set to sammy.

To set multiple variables, repeat the -e flag for each one:

If you’d like to pass in a file full of environment variables you can do that with the --env-file flag.

First, make the file with a text editor. We’ll open a new file with nano here, but you can use any editor you’re comfortable with:

We’re using .env as the filename, as that’s a popular standard for using these sorts of files to manage information outside of version control.

Write your KEY=value variables into the file, one per line, like the following:

Save and close the file. To save the file and exit nano, press CTRL+O, then ENTER to save, then CTRL+X to exit.

Now run the docker exec command, specifying the correct filename after --env-file:

The two variables in the file are set.

You may specify multiple files by using multiple --env-file flags. If the variables in the files overlap each other, whichever file was listed last in the command will override the previous files.

Common Errors

When using the docker exec command, you may encounter a few common errors:

Docker Run Name

The No such container error means the specified container does not exist, and may indicate a misspelled container name. Use docker ps to list out your running containers and double-check the name.

This not running message means that the container exists, but it is stopped. You can start the container with docker start container-name

The Container is paused error explains the problem fairly well. You need to unpause the container with docker unpause container-name before proceeding.

Conclusion

In this tutorial we learned how to execute commands in a running Docker container, along with some command line options available when doing so.

For more information on Docker in general, please see our Docker tag page, which has links to Docker tutorials, Docker-related Q&A pages, and more.

Docker Run Example

For help with installing Docker, take a look at How To Install and Use Docker on Ubuntu 20.04.

Comments are closed.