Docker is a very handy platform when it comes to managing large-scale applications on your system. It provides a container-based unit for deploying necessary applications. You can install docker and create container users in your Linux operating system. In this article, I will illustrate how to create a user in Docker in Ubuntu not using a dockerfile.
Key Takeaways
- Installing Docker in Ubuntu.
- Downloading and creating docker container.
- Adding a new user to docker
Requirements
- Need to be a root user or have root/sudo privileges to install Docker.
- Have Docker installed on Ubuntu.
- Need a Docker Container created on Ubuntu.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS
Create New User to Docker Container Without Docker File
A docker container stands for an environment that includes all the system tools, libraries, and settings to run software packages. It offers an OS-level infrastructure for uniform deployment and staging of applications. As it is similar to a virtualized OS environment, adding a new user to a docker container is also similar to adding a new user to the OS.
In this section, I will add a new docker container user called “jerry” to my Linux distribution, Ubuntu. To create a new user to the docker container without the docker file, you can follow the steps below:
- Firstly, open the Ubuntu terminal.
- Then, log in to your docker container with the following command.
docker exec -it container-tom /bin/bash
EXPLANATION- docker: Indicates docker commands.
- exec -it: Runs docker command inside the Docker container.
- container-tom: Name of the container.
- /bin/bash: The default shell for user log-in.
- Now, run the following command to add a new user.
adduser jerry
EXPLANATION- adduser: Adds a new user to the container.
- jerry: Name of the newly created user.
- Type a new password for your new user and press ENTER.
- Retype the password for confirmation and hit ENTER again.
- Continue pressing ENTER to skip the steps in the middle. However, you can give your desired information according to your need.
- Finally, type Y to confirm your given information.
- Now, to verify if the newly created user has access to your docker container, switch to the user account with the given command and press ENTER.
su - jerry
EXPLANATION- su -: Switches user account.
- jerry: Name of the user account to be switched.
- Move to the tomcat folder by running the command below.
cd /usr/local/tomcat
EXPLANATION- cd: Changes directory location.
- /usr/local/tomcat: Path to the Tomcat
- Now, list all the tomcat container files to see if the new user has access or not.
ls
EXPLANATION- ls: Lists the contents of the current directory.
In the image above, you can see that I have created a new docker user called “jerry” without using a docker file. You can check the new docker user account by switching to it and viewing the container files.
- How to Create Multiple Users in Linux? [2 Methods]
- Create a User in Ubuntu as Read-Only Access to Log Folder
- How to Create User Account in Ubuntu with Public Key
- Create A User in Ubuntu that Runs a Script and Logs Out [4 Steps]
Complementary Information
Besides, knowing about creating a new Docker container user on Ubuntu, you will find the below information helpful.
Installing Docker on Ubuntu
Although your Ubuntu repository contains a docker package, it may not be up to date. Therefore, to get the latest version I will download the docker package from the docker repository and install it on my system. Follow the steps below to install the latest docker package.
- First, open your Ubuntu terminal.
- Update existing packages on your system with the command below and press ENTER.
sudo apt update
- Run the following command to install the prerequisite packages.
sudo apt install apt-transport-https ca-certificates curl software-properties-common
EXPLANATION- sudo: Grants administrative privileges.
- apt: Manages available packages on the system.
- install: Installs packages on the system.
- apt-transport-https: Allows to use HTTP secured repositories.
- ca-certificates: Allows to check SSL-based authenticity and certificates.
- curl: Allows file transfer via various protocols.
- software-properties-common: Allows to manage your distribution and independent software.
- Then, run the given command to add the GPG key to your system
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
EXPLANATION- curl -fsSL: Silently downloads files from a URL and shows errors if they occur.
- https://download.docker.com/linux/ubuntu/gpg: URL to download Docker GPG key.
- sudo: Grants administrative privileges.
- apt-key add – : Downloads and adds OpenPGP keys for authentication.
- Now, add the Docker repository to your APT sources.
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
EXPLANATION- sudo: Grants administrative privileges.
- add-apt-repository : A script that allows to add an APT repository to sources.
- deb [arch=amd64]: Specifies the binary package repository for the amd64 architecture.
- https://download.docker.com/linux/ubuntu: URL of the Docker repository for
- focal stable: Specifies the Ubuntu release (Ubuntu 20.04, code-name: Focal Fossa) and the release channel for the intended packages.
- To check if you are about to install docker from the docker repository run the following command.
apt-cache policy docker-ce
EXPLANATION- apt-cache policy : Displays the pinning priorities and distribution properties of each package source.
- docker-ce: The package name for the open-source Docker Community Edition.
- Finally, install the docker environment by running the given command.
sudo apt install docker-ce
EXPLANATION- sudo: Grants administrative privileges.
- apt install: Installs the Docker package.
- docker-ce: Package name of the open-source Docker Community Edition.
- Upon installing, you can check the docker status with the command below.
sudo systemctl status docker
EXPLANATION- sudo: Grants administrative privileges.
- systemctl: Manages system services.
- status docker: Checks the status of the Docker service.
In the above image, I have installed the Docker service on my machine by following the above-mentioned steps. To view the status of the Docker service you can utilize the systemctl command as shown above.
- How to Create an FTP User in Ubuntu? [Step-by-Step]
- Create a New SFTP User in Ubuntu with a New SSH Key
- How to Create Group and Add User in Ubuntu? [2 Cases]
- Create FTP User for Specific Directory in Ubuntu [4 Steps]
Create Docker Container in Ubuntu
The docker environment where it provides OS-level virtualization for managing application packages is called a docker container. Containers are lightweight executable packages known as container images. These container images become docker containers when run on the docker engine.
In this section, I will create a docker container of the free and open-source server “Tomcat”. You can do the same by following the steps below:
- At first, go to your Ubuntu terminal.
- Download the latest version of Tomcat docker image with the following command.
docker pull tomcat:latest
EXPLANATION- docker: Indicates docker commands.
- pull: Downloads the image file.
- tomcat:latest: The latest version of the Tomcat image file.
- Then, retrieve the tomcat image ID using the following command.
sudo docker ps -a
EXPLANATION- sudo: Grants administrative privileges.
- docker: Indicates docker commands.
- ps -a: Lists docker image files.
- Finally, create a tomcat container called “container-tom” using the following command.
sudo docker run -d -p 8083:8083 --name container-tom 51c25da77baf
EXPLANATION- sudo: Grants administrative privileges.
- docker: Indicates docker commands.
- run -d: Runs docker container from an image.
- -p 8083:8083: Indicates port numbers for the tomcat server.
- –name container-tom: Given name of the tomcat server.
- 51c25da77baf: The tomcat image file ID.
- Now, to view the available containers in your system, run the command below.
docker ps
EXPLANATION- docker: Indicates docker commands.
- ps: Lists the available docker containers.
Upon completing the given steps, you can see that I have successfully created a tomcat docker container in my system and named it “container-tom”. You can view the created docker container information by running the “docker ps” command.
- How to Create a Jenkins User on Ubuntu? [2 Methods]
- How to Create MySQL User in Ubuntu? [2 Cases]
- Create User Without Home Directory in Ubuntu in Just 3 Steps
- How to Create a Root User in Ubuntu [Step-by-Step]
Running Docker As a Non-root User in Ubuntu
By default, a docker engine on your Linux OS can only be run as the root user. However, you can give access to running the docker engine to other users as well. In this example, I will enable my currently logged-in user to run docker. You can do the same by following the process below:
- At first, launch the Ubuntu terminal.
- Now, run the following command on your command line.
sudo usermod -aG docker anonnya
EXPLANATION- sudo: Grants administrative privileges.
- usermod: Modifies an existing user account.
- -aG: Adds a user to the specific group.
- docker: Name of the group to add a specified user.
- anonnya: Username of the user to be added to the docker group.
- Finally, restart your system using the following command.
reboot
After restarting the system, you will be able to run the docker commands without the application of the sudo keyword. Therefore, you will have permission to run the docker engine as a regular user. In this case, my user account “anonnya” can run docker commands without the sudo keyword.
Conclusion
In this article, I have demonstrated the basic concepts of Docker container technology. This tutorial will enable you to install the Docker engine on your Linux machine and create a desired container from a specific Docker image. After creating a docker container you will be able to add new users to it as per your requirement. I hope this article will help you get familiar with the Docker technology in Linux and improve your overall experience.
People Also Ask
Can I create a Docker container without a Docker image?
No. You cannot create a docker container without docker images as the image contains all the necessary instructions for creating a docker container.
How to use Docker without Docker?
You cannot use Docker without installing Docker on your machine. Since docker is a containerization platform it needs the Docker daemon, to manage containerization.
How do I specify a user for a container?
While running a docker container you can specify a user with the option--user
. You will need to mention the username followed by the option along the image file.
What is user 0 in the Dockerfile?
The user ID 0 in the Docker file indicates the root user of the system. Additionally, it means that the instructions within the dockerfile and the docker commands are run by the root.
How to get Docker user ID?
To get the docker user ID, first, you will need to log in to the container. Inside the container, you can run the id command to view the docker user ID.
How do I specify a user in the Dockerfile?
To specify a user in the Dockerfile you will need to use the USER instruction followed by the desired user name. Furthermore, you may need to adjust permissions to enable the specified user actions.
How to get a Docker username and password?
The docker will not provide a specific username and password for the user. By default, it is the root user who has access to the installed Docker engine.
What is the default docker user?
The default Docker user is the root user of the system. Only the root user has permission to run the docker commands and create desired containers.
Related Articles
- How to Create User and Add to Sudo in Ubuntu? [2 Methods]
- 2 Ways to Create a User With Access to a Drive in Ubuntu
- How to Create a New User in Ubuntu by GUI? [Step-by-Step]
- 2 Ways to Create User Account in Ubuntu Using Terminal
- How to Create Home Directory for Existing User in Ubuntu
- Create User with UID and GID in Ubuntu? [3 Scenarios]
- How to Create a Sudo User in Ubuntu? [Step-by-Step]
FUNDAMENTALS A Complete Guide for Beginners