How to Create a User in Docker Container Without Docker File?

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

Process Flow Chart

Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTSFlowchart of how to create docker container user without docker file

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:

  1. Firstly, open the Ubuntu terminal.
  2. 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.

    Logging into docker container.

  3. 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.
  4. Type a new password for your new user and press ENTER.
  5. Retype the password for confirmation and hit ENTER again.
  6. Continue pressing ENTER to skip the steps in the middle. However, you can give your desired information according to your need.
  7. Finally, type Y to confirm your given information.
  8. 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.
  9. 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
  10. 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.

    Adding a new user to docker container.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.



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.

  1. First, open your Ubuntu terminal.
  2. Update existing packages on your system with the command below and press ENTER.
    sudo apt update
    EXPLANATION
    • sudo: Grants administrative privileges.
    • apt: Manages available packages on the system.
    • update: Updates available packages.

    Updating available packages in system.

  3. 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.

    Installing the prerequisite packages.

  4. 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.

    Adding the GPG key to your system.

  5. 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.

    Adding the Docker repository to your APT sources.

  6. 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.

    Verifying docker repository.

  7. 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.

    Installing docker in system.

  8. 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.

    Checking docker status.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.



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:

  1. At first, go to your Ubuntu terminal.
  2. 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.

    Downloading latest tomcat docker image file.

  3. 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.

    Retrieving the tomcat image ID.

  4. 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.
  5. 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.

    Create a new docker container and then list available 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.



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:

  1. At first, launch the Ubuntu terminal.
  2. 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.
  3. Finally, restart your system using the following command.
    reboot

    Adding non-root user to docker.

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

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment